package com.shuzijun.leetcode.editor.cn;
public class Sqrtx{
    public static void main(String[] args) {
         Solution solution = new Sqrtx().new Solution();
        
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int mySqrt(int x) {
        if (x == 0) {
            return 0;
        }
        int low = 1;
        int high = x/2+1;
        while (low <= high){
            int mid = low + (high - low) / 2;
            long mid2 = (long) mid*mid;
            if (mid2 == x){
                return mid;
            } else if (mid2 < x) {
                long mid22 = ((long)mid+1)*(mid+1);
                if (mid22 <= x){
                    low = mid+1;
                }else {
                    return mid;
                }
            }else {
                high = mid-1;
            }
        }
        return -1;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}